'use strict';

const Controller = require('egg').Controller;

/**
 * 创建者: caoyq
 * 创建时间: 2019年08月18日 20:14:02
 * 功能描述: 菜单管理控制器
 */
class SysMacAddrController extends Controller {

  /**
   * macAddr列表
   */
  async findMacAddrList() {
    const {
      ctx,
      service
    } = this;
    const user_id = ctx.curAdminUser && ctx.curAdminUser.id || '';
    if (!user_id) {
      ctx.response.failByCode('0003');
      return;
    }
    const options = ctx.request.body;
    if (options.startDate && options.endDate) {
      options.endDate = await ctx.getDatePlus1(options.endDate);
    }
    const result = await service.admin.sysMacAddr.findMacAddrList(options);
    // 添加操作日志
    ctx.writeSysOperationLogs('查询macAddr列表', ctx.getSystemLogType('QUERY'), {
      tableName: 'macAddr信息表'
    });
    ctx.response.success(result);
  }
  /**
   * 添加macAddr
   */
  async addMacAddr() {
    const {
      ctx,
      service
    } = this;
    const user_id = ctx.curAdminUser && ctx.curAdminUser.id || '';
    if (!user_id) {
      ctx.response.failByCode('0003');
      return;
    }
    const macAddrparams = ctx.request.body;
    const macAddrModel = {};
    if (macAddrparams.remarks) {
      macAddrModel.remarks = macAddrparams.remarks;
    }
    macAddrModel.mac_addr = macAddrparams.macAddr;
    macAddrModel.creator = user_id;
    macAddrModel.status = 1;
    macAddrModel.create_time = ctx.getDate_YYYYMMDDHHmmss();
    const codeInfo = await service.admin.sysMacAddr.findMacAddrCode(macAddrparams.macAddr);
    if (codeInfo.length > 0) {
      ctx.response.failByCode('1001', '', '该机器码已存在！');
      return;
    }
    await service.admin.sysMacAddr.addMacAddr(macAddrModel);
    ctx.response.success();
    // 添加操作日志
    const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】添加了addMacAddr【${JSON.stringify(macAddrModel)}】`;
    ctx.writeSysOperationLogs('添加addMacAddr', ctx.getSystemLogType('CREATE'), {
      curAdminUser: ctx.curAdminUser,
      des
    });
  }

  /**
   * 删除macAddr
   */
  async delMacAddr() {
    const {
      ctx,
      service
    } = this;
    const user_id = ctx.curAdminUser && ctx.curAdminUser.id || '';
    if (!user_id) {
      ctx.response.failByCode('0003');
      return;
    }
    const id = ctx.request.body.id;
    if (!id) {
      ctx.response.failByCode('0001');
      return;
    }
    await service.admin.sysMacAddr.delMacAddr(id);
    ctx.response.success();
    const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】删除的macAddr的id为【${id}】`;
    ctx.writeSysOperationLogs('删除macAddr', ctx.getSystemLogType('DELETE'), {
      curAdminUser: ctx.curAdminUser,
      des
    });
  }

  /**
   * 修改macAddr
   */
  async updateMacAddr() {
    const {
      ctx,
      service
    } = this;
    const user_id = ctx.curAdminUser && ctx.curAdminUser.id || '';
    if (!user_id) {
      ctx.response.failByCode('0003');
      return;
    }
    const macAddrparams = ctx.request.body;
    const macAddrModel = {};
    if (!macAddrparams.id) {
      ctx.response.failByCode('0001');
      return;
    }
    if (macAddrparams.remarks) {
      macAddrModel.remarks = macAddrparams.remarks;
    }
    if (macAddrparams.status) {
      macAddrModel.status = parseInt(macAddrparams.status);
    }
    macAddrModel.creator = user_id;
    macAddrModel.update_time = ctx.getDate_YYYYMMDDHHmmss();
    await service.admin.sysMacAddr.updateMacAddr(macAddrparams.id, macAddrModel);
    ctx.response.success();
    const des = `【${ctx.curAdminUser.realName}】在【${ctx.getDate_YYYY_MM_DD_HH_mm_ss()}】修改的macAddr的id为【${macAddrparams.id}】`;
    ctx.writeSysOperationLogs('修改macAddr', ctx.getSystemLogType('DELETE'), {
      curAdminUser: ctx.curAdminUser,
      des
    });
  }
}

module.exports = SysMacAddrController;